﻿#region Library
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
#endregion

namespace SmsControl
{
    class SeperateString
    {
        public void remove_r(ref string[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = a[i].TrimEnd('\r');
            }
        }

        public DateTime parsedatetime(string s)
        {
            DateTime dt; //11/10/25,13:15:56+28
            s = s.Replace("+28", "");
            string[] temp = s.Split(',');
            string date = temp[0];
            string[] tempdate = date.Split('/');

            string time = temp[1];
            string[] temptime = time.Split(':');

            int day = (Int16.Parse(tempdate[2]));
            int month = (Int16.Parse(tempdate[1]));
            int year = (Int16.Parse(tempdate[0])) + 2000;

            int hour = (Int16.Parse(temptime[0]));
            int minute = (Int16.Parse(temptime[1]));
            int second = (Int16.Parse(temptime[2]));

            dt = new DateTime(year, month, day, hour, minute, second);
            return dt;
        }

        public SMS getmessage(string s)
        {
            SMS m = new SMS();

            string[] t = s.Split('\"');
            m.Number = t[3];
            m.Number = m.Number.Replace("+84", "0");

            m.Date = parsedatetime(t[5]);

            m.Content = t[6];
            return m;
        }

        public List<SMS> parsestring(string input)
        {
            List<SMS> messages = new List<SMS>();

            string[] temp = Regex.Split(input, "\r\n");
            temp[0] = "";
            temp[temp.Length - 1] = "";

            remove_r(ref temp);

            List<string> temp2 = new List<string>();
            
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].StartsWith("+CMGL"))
                {
                    temp2.Add(temp[i] + temp[i + 1]);
                }
            }

            SMS m = new SMS();
            foreach (string s in temp2)
            {
                m = getmessage(s);
                messages.Add(m);
            }

            return messages;
        }

        public string Modem_Name(string s)//Return a string name of modem
        {
            string modem_Name = "";
            string[] buffer = Regex.Split(s, "\r\n");
            modem_Name = buffer[1];
            return modem_Name;
        }

        public string Sim_Serie(string s)
        {
            string sim_Serie = "";
            string[] buffer = Regex.Split(s, "\"");
            sim_Serie = buffer[1];
            return sim_Serie;
        }

        //"+CMT: \"+841992017940\",,\"12/05/25,16:12:50+28\"\r\n.?\",mgdaj"
        //+CMT: \"+841992017940\",,\"12/05/25,15:05:02+28\"\r\nMgdjwakgt
        public SMS newMessage(string s)
        {
            SMS result = new SMS();
            
            string[] buffer = Regex.Split(s, "\r\n");
            result.Content = buffer[1];//content
            string[] buffer_2 = Regex.Split(buffer[0], "\"");
            result.Number = buffer_2[1].Replace("+84", "0");//number
            result.Date = parsedatetime(buffer_2[3]);//date

            return result;
        }
    }
}
